package com.spring.mvc.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.spring.mvc.exception.StringFormatExcption;

/**
 * 字符串操作工具类
 * @author liujian
 *
 */
public class StringUtil {

	/**
	 * 字符串左边补零
	 * 例：将字符串"ABC"用"0"补足8位，结果为"00000ABC"
	 * @param orgStr 原始字符串
	 * @param fillWith 用来填充的字符
	 * @param fixLen 固定长度
	 * @return
	 */
	public static String fillLeft(String orgStr, char fillWith, int fixLen) {

		return fillStr(orgStr, fillWith, fixLen, true);

	}
	
	public static String LeftPad(String s, String pad, int len) {
	    int l = len - s.getBytes().length;
	    String ss = s;
	    for (int i = 0; i < l; ++i)
	      ss = pad + ss;

	    return ss;
	  }
	
	private static String fillStr(String orgStr, char fillWith, int fixLen,
			boolean isLeft) {

		int toFill = fixLen - orgStr.length();

		if (toFill <= 0)
			return orgStr;

		StringBuilder sb = new StringBuilder(orgStr);
		for (; toFill > 0; toFill--) {
			if (isLeft)
				sb.insert(0, fillWith);
			else
				sb.append(fillWith);
		}

		return sb.toString();

	}
	
	/**
	 * 判断字符串是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		if (null == str || "" == str || str.length() == 0) {
			return true;
		}
		return false;
	}

	/**
	 * 判断字符是否不为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	/**
	 * 将字符串的左边补全
	 * 
	 * @param str
	 * @param length
	 * @return
	 * @throws StringFormatExcption
	 */
	public static String leftCompletion(String str, int length)
			throws StringFormatExcption {
		StringBuilder sb = new StringBuilder();
		int l = str.length();
		if (l < length) {
			for (int i = 0; i < length - l; i++) {
				sb.append("0");
			}
			sb.append(str);
		} else if (l == length) {
			return str;
		} else {
			throw new StringFormatExcption("原字符串长度大于：" + length);
		}
		return sb.toString();
	}

	/**
	 * 判断字符串是否在指定长度之内
	 */
	public static boolean isAppointLength(String str, int minLength,
			int maxLength) {
		if (str.trim().length() > minLength && str.trim().length() < maxLength) {
			return true;
		}
		return false;
	}

	public static boolean isEmail(String str) {
		String reg = "^[a-zA-Z0-9]+?@[a-zA-Z0-9]+?\\.[a-zA-Z0-9]+?$";
		Pattern pattern = Pattern.compile(reg);
		Matcher matcher = pattern.matcher(str);
		if (matcher.matches()) {
			return true;
		}
		return false;
	}

	/**
	 * 替换
	 * 
	 * @param strSource
	 * @param strFind
	 * @param strReplace
	 * @return
	 */
	public static String replaceString(String strSource, String strFind,
			String strReplace) {
		String strTemp = strSource;
		StringBuffer sb = new StringBuffer();

		if (strTemp != null && strFind != null && strReplace != null) {
			int pos;
			while ((pos = strTemp.indexOf(strFind)) != -1) {
				sb.append(strTemp.substring(0, pos));
				sb.append(strReplace);
				strTemp = strTemp.substring(pos + strFind.length());
			}
			sb.append(strTemp);
			return new String(sb);
		}
		return strSource;
	}

	public static String[] splitter(String code, String delim) {
		int size = CountStrNum(code, delim);
		return splitter(code, delim, size);
	}

	public static String[] splitter(String code, String delim, int length) {
		int pos = -1;
		int begin = 0;
		String[] s = new String[length];
		int count = 0;
		while ((pos = code.indexOf(delim, pos + 1)) != -1) {
			s[count] = code.substring(begin, pos);
			begin = pos + 1;
			++count;
		}
		s[count] = code.substring(begin, code.length());
		++count;
		return s;
	}

	public static int CountStrNum(String source, String delim) {
		int pos = -1;
		int begin = 0;
		int count = 1;
		while ((pos = source.indexOf(delim, begin)) >= 0) {
			++count;
			begin = pos + 1;
		}
		return count;
	}

	public static void printStrArr(String[] items) {
		for (String str : items) {
			System.out.println(str);
		}
	}
}